/*
 * HitTheLines.h
 *
 *  Created on: 2011-03-29
 *      Author: lpieczkowski
 */

#ifndef HITTHELINES_H_
#define HITTHELINES_H_

#include <FApp.h>
#include <FBase.h>
#include <FSystem.h>
#include <FUi.h>
#include <FGraphics.h>
#include <FGraphicsOpengl.h>
#include <FMedia.h>
#include <string>
#include <stack>


static Osp::Graphics::Opengl::GLfloat backRectPoints[] = {
	 0.8f, -1.0f,
	 0.8f, -0.7f,
	 1.0f, -1.0f,
	 1.0f, -0.7f
};

static Osp::Graphics::Opengl::GLfloat backRectCoords[] = {
	0.0f, 0.0f,
	0.0f, 1.0f,
	1.0f, 0.0f,
	1.0f, 1.0f
};


/**
 * [Lines] application must inherit from Application class
 * which provides basic features necessary to define an application.
 */
class HitTheLines:
	public Osp::App::Application,
	public Osp::System::IScreenEventListener,
	public Osp::Base::Runtime::ITimerEventListener,
	public Osp::Ui::ITouchEventListener
{

/***************************************************************
 *
 *
 * METHODS
 *
 *
 ***************************************************************/

public:

	static HitTheLines* getInstance();

	//Application must have a factory method that creates an instance of itself.
	static Osp::App::Application* CreateInstance(void);

	//Standard constructor / destructor
	HitTheLines();
	virtual ~HitTheLines();

	// Called when the application is initializing.
	bool OnAppInitializing(Osp::App::AppRegistry& appRegistry);

	// Called when the application is terminating.
	bool OnAppTerminating(Osp::App::AppRegistry& appRegistry, bool forcedTermination = false);

	// Called when the application's frame moves to the top of the screen.
	void OnForeground(void);

	// Called when this application's frame is moved from top of the screen to the background.
	void OnBackground(void) {}

	// Called when the system memory is not sufficient to run the application any further.
	void OnLowMemory(void) {}

	// Called when the battery level changes.
	void OnBatteryLevelChanged(Osp::System::BatteryLevel batteryLevel) {}

	// Called when the screen turns on.
	void OnScreenOn (void) {}

	// Called when the screen turns off.
	void OnScreenOff (void) {}

	void OnTimerExpired(Osp::Base::Runtime::Timer& timer);

    void OnTouchPressed(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo);
    void OnTouchReleased(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo){}
    void OnTouchMoved(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo){}

    void OnTouchIndicated(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& startPosition, const Osp::Graphics::Point& currentPoint) {}
    void OnTouchLongPressed(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo) {}
    void OnTouchDoublePressed(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo) {}
    void OnTouchFocusIn(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo) {}
    void OnTouchFocusOut(const Osp::Ui::Control& source,
            const Osp::Graphics::Point& currentPosition, const Osp::Ui::TouchEventInfo & touchInfo) {}

    void GoToMainMenu();

	static void updateTextureSize(int& iWidth, int& iHeight);

    void PushScreen(HitTheLines* newScreen);
    void PopScreen();
    static void LoadTexture(Osp::Base::String filename, Osp::Graphics::Opengl::GLuint* texture);

//Interface Methods
public:

    virtual void NavigateBack();
    virtual void Initialize(void);
    virtual void UpdateLogic(void);
    virtual void Render(void);
    virtual void PointerPressed(float x, float y);
    virtual void CleanUp();
    virtual std::string GetScreenName();

private:

    //Base Class Methods
	bool InitEGL();
	bool InitGL();
	bool InitFonts();
	void LoadCommonTextures();
	void RenderBackKey();


    /***************************************************************
     *
     *
     * MEMBERS
     *
     *
     ***************************************************************/

public:

	//Fonts
    Osp::Graphics::Font font;

    int globalHeight;
    int globalWidth;

    Osp::System::Vibrator Vibrator;

private:

	static HitTheLines* instance;

    Osp::Graphics::Opengl::EGLDisplay eDisplay;
    Osp::Graphics::Opengl::EGLSurface eSurface;
    Osp::Graphics::Opengl::EGLConfig  eConfig;
    Osp::Graphics::Opengl::EGLContext eContext;

    Osp::Base::Runtime::Timer* globalTimer;

    std::stack<HitTheLines*> ScreenStack;
    std::string ScreenName;

    Osp::Graphics::Canvas* fontCanvas;
    Osp::Graphics::EnrichedText* fontEnrichedText;
    Osp::Graphics::Opengl::GLuint backKeyTexture;
    Osp::Graphics::Opengl::GLuint backKeyGlowTexture;

    long long curTime;
    long long prevTime;
    int frameTime;

};

#endif /* HITTHELINES_H_ */
